package com.zjzywl.smoke.repository;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.zjzywl.smoke.entity.OrgDev;


@Repository
public interface OrgDevRepository extends JpaRepository<OrgDev, Integer>{
	@Query("select u from OrgDev u where u.organization.id=:id")
	List<OrgDev> findByOrgid(@Param("id") Integer id);
	
	@Query("select number from OrgDev u where u.organization.id=:id")
	List<Integer> countDevicesByOrg(@Param("id") Integer id);
	
	@Modifying
	@Transactional
	@Query("delete from OrgDev u where u.organization.id=:id")
	void deleteByOrg(@Param("id") Integer orgid);
	
	@Modifying
	@Transactional
	@Query("delete from OrgDev u where u.device.id=:id")
	void deleteByDev(@Param("id") Integer Devid);
	
	@Query("select a.device.deviceType,count(a) from OrgDev a,SysUser b where a.organization=b.org and b.userId=:userId group by a.device.deviceType having count(a)>0")
	List deviceTypeCountByUser(@Param("userId") Integer userId);
}
